package UIFree.abox
{
	import UIFree.ObjectEvent;
	import UIFree.uif_Timer;
	import UIFree.uif_component;
	
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class uif_moduleBox extends uif_abox {
		private 	var _previewURL		: String;
		private 	var _selectEvent	: Object;
		private 	var _dataModule		: Object;
		private		var _rows			: int;
		protected 	var _filterList		: Array;
		
		public static const EXTERNALGROUPSELECT: String = 'EXTERNALGROUPSELECT';
		
		public function uif_moduleBox(parent:uif_component, a_dataModule: Object, a_previewURL: String, a_selectEvent: Object, 
									ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, atabwidth:int=0, a_rows: int=0,
									styleClass:String='', countInfo:Boolean=true) {
			_dataModule 	= a_dataModule;
			_previewURL		= a_previewURL;
			_selectEvent	= a_selectEvent;
			_rows 			= a_rows;
			super(parent, _dataModule.groups, ax, ay, awidth, aheight, atabwidth, styleClass, countInfo);
			_itemsLayer.addEvent(Event.SELECT, doItemSelect);
			uif_Timer.one(10, function(e: Event): void {defaultSelectSet()});
		}
		
		public function get sexUser(): int {
			return 0;
		}
		
		protected function defaultSelectSet(): void {
			function updateDefault(e: Event = null): void {
				_dataModule.groups.forEach(function(item:*, index:int, array:Array): void {
					if (item.id == _dataModule.Default[sexUser].group) {
						tabs.selectIndex = index;
						if (_dataModule.Default[sexUser].hasOwnProperty('id')) {
							for (var i:int; i<_itemsLayer.items.length; i++) {
								if (_itemsLayer.items[i].object && 
									(_itemsLayer.items[i].object.id == _dataModule.Default[sexUser].id)) {
									_itemsLayer.items[i].control.select();
									return;
								}
							}
						}
					}
				});				
			}
			if (_dataModule.hasOwnProperty('Default')) updateDefault();
		}
		
		protected function doItemSelect(e: ObjectEvent): void {
			e.stopPropagation();
			if (e.object && e.object.object) {
				if (_selectEvent is String)	
					dispatchEvent(new ObjectEvent(_selectEvent as String, e.object.object));
				else if (_selectEvent is Function)
					(_selectEvent as Function)(e.object.object);
			}
		}
		
		public function assignContent(createItemFunc: Function): void {
			var v_rows		: int = (_rows > 0)?_rows:0;
			var item_width	: int;
			var condition	: Boolean = true;
			function isSmallList(): Boolean {
				return (_filterList.length < (Math.floor(_itemsLayer.displayRect.width / item_width) * v_rows));
			}
			
			if (_rows == 0)
				while (condition && (v_rows < 4)) {
					v_rows++;
					item_width 	= _itemsLayer.calcFrameWidth(v_rows);
					condition 	= !isSmallList();
				}
			
			item_width 	= _itemsLayer.calcFrameWidth(v_rows);
			
			while (isSmallList()) _filterList.push(null);
			_itemsLayer.assignList(_filterList, item_width, createItemFunc, v_rows);
			_itemsLayer.scrollStep = _itemsLayer.countDisplay / v_rows;
		}
		
		override protected function doChangeSelect(tabIndex: int): void {
			if (tabs.selectItem.object.id < 1000) { 
				_filterList = _dataModule.list.filter(function(item: Object, index: int, arra: Array): Boolean {
					return item.group == tabs.selectItem.object.id;
				}); 
				_filterList.sortOn('id', Array.NUMERIC | Array.DESCENDING);
				assignContent(createListItem);
			} else {
				dispatchEvent(new ObjectEvent(EXTERNALGROUPSELECT, {
					group		: tabs.selectItem.object,
					callback	: function(a_filterList: Array): void {
						_filterList = a_filterList;
						_filterList.sortOn('id', Array.NUMERIC | Array.DESCENDING);
						assignContent(createListItem);
					}
				}));
				/*
				clt.internalServer.oneQuery('getGroupTmpls', tabs.selectItem.object.id, function(a_data: Object): void {
					var g_list: Array = a_data.response[0] as Array;
					g_list = g_list?g_list:[];
					_filterList = _dataModule.list.filter(function(item: Object, index: int, arra: Array): Boolean {
						return g_list.some(function(g_item: Object, index: int, arr: Array): Boolean {
							return g_item.tmpl_id == item.id;
						});
					}); 
					assignContent(createListItem);					
				});
				*/
			}
		}
		
		protected function createListItem(_scroll: uif_component, inner: Rectangle, index: int): uif_previewItem {
			return new uif_previewItem(index, _scroll, inner.x, inner.y, inner.width, inner.height, 
										_filterList[index], _itemsLayer.style['unselect'], _previewURL);
		}
	}
}